/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.ProcurementBP;

import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import merlion.frm.bpm.entity.SupplierInvoice;
import merlion.frm.bpm.entity.SupplierPaymentRecord;
import merlion.frm.bpm.session.ProcurementBPSessionBeanLocal;

/**
 *
 * @author Kenn
 */
@ManagedBean (name = "sprManagerBean")
@ViewScoped
public class SupplierPaymentManagerBean {
    
    @EJB
    private ProcurementBPSessionBeanLocal pbpSessionBean;
    
    private SupplierPaymentRecord spr;
    private SupplierPaymentRecord selectedSpr;
    private Collection<SupplierPaymentRecord> sprs;
    private String statusMsg;

    //--------------------------------------------------------------------------
    //                              Constructor
    //--------------------------------------------------------------------------
    public SupplierPaymentManagerBean() {
        
        spr = (SupplierPaymentRecord) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("spr");
        statusMsg = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("statusMsg");
        
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("spr");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("statusMsg");
        
        if(spr == null){
            spr = new SupplierPaymentRecord();           
            spr.setPaymentAmount(0.0);
            spr.setPaymentMethod(null);
            spr.setSupplierInvoice(new SupplierInvoice());
            spr.setSupplierReceipt(null);
        }
    }
    @PostConstruct    
    public void init(){
        spr.setSupplierPaymentRecordId(pbpSessionBean.generateSupplierPaymentRecordId());
    }
    
    //--------------------------------------------------------------------------
    //                              Manager Bean Methods
    //--------------------------------------------------------------------------
    
    public String createSupplierPaymentRecord(){
        
        // Validate
        boolean valid = true;
        if (spr.getPaymentMethod() == null || spr.getPaymentMethod().equals("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Payment Method cannot be empty", ""));
            valid = false;
        }
        if ((double) spr.getPaymentAmount() == 0) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Payment amount cannot be zero", ""));
            valid = false;
        }
        if ((double) spr.getPaymentAmount() != (double)spr.getSupplierInvoice().getInvoiceAmount()) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Full payment must be made", ""));
            valid = false;
        }
        
        if(!valid){
            return null;
        }
        
        spr = pbpSessionBean.saveSupplierPaymentRecord(spr);
        if(spr != null){
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "Supplier Payment Record created successfully.");
        } else {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "Supplier Payment Record failed to create.");
        }
               
        return "viewSupplierPaymentRecordStatus?faces-redirect=true";
    }
    
    public String viewSupplierPaymentRecord(){
        spr = selectedSpr;
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("spr", spr);
        
        return "viewSupplierPaymentRecordDisplay?faces-redirect=true";
    }
    
    
    public Collection<SupplierInvoice> getAllUnpaidSupplierInvoice(){
        return pbpSessionBean.getAllUnpaidSupplierInvoice();
    }
    
    //--------------------------------------------------------------------------
    //                              Setter and Getter
    //--------------------------------------------------------------------------

    public SupplierPaymentRecord getSpr() {
        return spr;
    }

    public void setSpr(SupplierPaymentRecord spr) {
        this.spr = spr;
    }

    public Collection<SupplierPaymentRecord> getSprs() {
        return pbpSessionBean.getAllSupplierPaymentRecord();
    }

    public void setSprs(Collection<SupplierPaymentRecord> sprs) {
        this.sprs = sprs;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }

    public SupplierPaymentRecord getSelectedSpr() {
        return selectedSpr;
    }

    public void setSelectedSpr(SupplierPaymentRecord selectedSpr) {
        this.selectedSpr = selectedSpr;
    }
    
}
